#!/bin/sh
#
# cassandrad This shell 
#	
#
# chkconfig: - 70 36
# description:	cassandrad
# processname: cassandrad

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# this script only apply for one cassandra instace on one host
cassandra_path=/lenovo/app/cassandra/
cassandra_pid=/lenovo/data/cassandra/cassandra.pid

ulimit -c unlimited
ulimit -n 10240

start(){
    if [ -f $cassandra_pid ];then
        c_pid=`cat $cassandra_pid`
        if [ `ps -ef | grep -c $c_pid`  -ge 2 ]; then
            echo "cassandra is running ..."
            return 0
        else
            rm -rf $cassandra_pid
        fi
    fi
    
    cd $cassandra_path/bin
    ./cassandra -R -p $cassandra_pid > /dev/null 2>&1
    
    c_pid=`cat $cassandra_pid`
    if [ `ps -ef | grep -c $c_pid`  -ge 2 ]; then
        echo "cassandra is started"
    else
        echo "cassandra start failed"
    fi
}

stop(){
    if [ -f $cassandra_pid ];then
        kill `cat $cassandra_pid`
        sleep 1
        if [ -f $cassandra_pid ];then
            echo "cassandra stop failed"
        else
            echo "cassandra is stopped"
        fi
    else
        echo "cassandra has stopped"
    fi
}

restart(){
    stop
    start
}

status(){
    if [ -f $cassandra_pid ];then
        c_pid=`cat $cassandra_pid`
        if [ `ps -ef | grep -c $c_pid`  -ge 2 ]; then
            echo "cassandra is running..."
        else
            echo "cassandra is stopped"
        fi
    else
        echo "cassandra is stopped"
    fi
    return 0
}

show_usage() {
    echo $"Usage: $0 {start|stop|status|restart}"
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart)
    restart
    ;;
  *)
    show_usage
    exit 2
esac

exit $?
